package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import com.lxj.xpopup.XPopup
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityAccountAddEditBinding
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.ui.newpage.pop.ConfimPop
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-03-28 18 GMT+8
 * @desc :
 */
class AccountAddEditActivity : VMBaseActivity<ActivityAccountAddEditBinding, WalletViewModel>() {
    override val binding by viewBinding(ActivityAccountAddEditBinding::inflate)
    override val viewModel by viewModels<WalletViewModel>()


    override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {

        val type = intent.getIntExtra("type", 0)
        if (type == 1)
            menuInflater.inflate(R.menu.account_delete, menu)
        return super.onCompatCreateOptionsMenu(menu)
    }


    override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_share_it -> {
                val accountId = intent.getStringExtra("accountId")
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asCustom(ConfimPop(this) {
                        viewModel.deleteAccount(accountId ?: "")
                    })
            }

        }
        return super.onCompatOptionsItemSelected(item)
    }


    override
    fun onActivityCreated(savedInstanceState: Bundle?) {
        val type = intent.getIntExtra("type", 0)

        if (type == 1) {

            val account = intent.getStringExtra("account")
            val name = intent.getStringExtra("name")

            val isDefault = intent.getIntExtra("isDefault", 0)

            binding.etAccount.setText(account)
            binding.etName.setText(name)
            binding.rcbIsDefault.isChecked = isDefault == 1

        }


        binding.rtvSave.setOnClickListener {
            var account = binding.etAccount.text.toString()
            var name = binding.etName.text.toString()

            if (TextUtils.isEmpty(account)) {
                toastOnUi("请输入支付宝账号")
                return@setOnClickListener
            }

            if (TextUtils.isEmpty(name)) {
                toastOnUi("请输入姓名")
                return@setOnClickListener
            }
            val type = intent.getIntExtra("type", 0)
            if (type == 0)
                viewModel.addAccount(account, name, if (binding.rcbIsDefault.isChecked) 1 else 0)
            else {
                val accountId = intent.getStringExtra("accountId")
                viewModel.editAccount(
                    accountId ?: "",
                    account,
                    name,
                    if (binding.rcbIsDefault.isChecked) 1 else 0
                )
            }

        }


        viewModel.addAccountResult.observe(this) { data ->
            if (data.status == 200) {
                toastOnUi("添加成功")
                finish()
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }

        viewModel.editAccountResult.observe(this) { data ->
            if (data.status == 200) {
                toastOnUi("修改成功")
                finish()
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }
        viewModel.deleteAccountResult.observe(this) { data ->
            if (data.status == 200) {
                toastOnUi("删除成功")
                finish()
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


    }

}